package in.muton.edushare.components.tasks.electives;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.coursepages.CoursePage;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.ElectivePreferenceDAO;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Users;

public class CompletedCommonElectivePanel extends EdusharePanel {

	public CompletedCommonElectivePanel(String id,
			final IModel<Curricula> currModel, IModel<Users> userModel,
			final List<Integer> groupIDs, final int semester) {
		super(id);
		CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());
		add(new Label("electiveGroup", getTitleString(groupIDs) + " of "
				+ currModel.getObject().getCurriculaName()+" for semester "+ (semester+1) +"."));

		int selectNum = groupIDs.size();
		int totalNum = cDAO.getElectiveCourses(currModel.getObject(), semester,
				groupIDs.get(0)).size();
		add(new Label("selectNum", "" + selectNum));
		add(new Label("totalNum", "" + totalNum));

		CayenneList<Courses> courseList = new CayenneList<Courses>(cDAO
				.getElectiveCourses(currModel.getObject(), semester, groupIDs
						.get(0)));

		add(new ListView<IModel<Courses>>("choiceListLV", courseList) {
			@Override
			protected void populateItem(final ListItem<IModel<Courses>> arg0) {
				Link<Void> l;
				ElectivePreferenceDAO eDAO = new ElectivePreferenceDAO(
						getORMInterface());
				CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());

				arg0.add(new Label("serialNum", (arg0.getIndex()+1) + ""));
				arg0.add(l = new BookmarkablePageLink<Void>("courseLink",
						CoursePage.class, EntityPageParams.getParams(arg0
								.getModelObject())));
				l.add(new Label("courseName", arg0.getModelObject().getObject()
						.getCourseName()));
				arg0.add(new Label("preference", ""+eDAO
						.getPreference(getLoggedInUser(), cDAO.getSemCourse(
								currModel.getObject(), semester, groupIDs
										.get(0), arg0.getModelObject()
										.getObject()), groupIDs.get(0)).getPreferenceNumber()));
			}

			private static final long serialVersionUID = 6484186314418893973L;
		});

	}

	public static String getTitleString(List<Integer> groupIDs) {
		String s = "";
		int size = groupIDs.size();
		int index = 0;
		for (Integer in : groupIDs) {
			if(index == size - 1)
				s += " "+(in+1)+" ";
			else if(index == size - 2)
				s += " "+(in + 1) + " and";
			else
				s += " " + (in+1) + ",";
			index ++;
		}
		s = "Electives " + s;
		return s;
	}

	private static final long serialVersionUID = 4765449397620107548L;
}
